package com.wchen.gdoc.generate.mapping.resolver;

import com.wchen.gdoc.generate.mapping.data.ParamData;
import com.wchen.gdoc.generate.resource.structure.ObjectStructure;
import com.wchen.gdoc.generate.util.DataTypeUtil;
import com.wchen.gdoc.generate.util.StructureUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 列表类型结构解析
 *
 * @author  wchen
 * @date    2019/11/7
 */
public class ListTypeStructureResolver extends AbsObjectStructureResolver implements TypeStructureResolver {
    private final static Logger LOGGER = LoggerFactory.getLogger(ListTypeStructureResolver.class);

    public ListTypeStructureResolver() {
        super();
    }

    /**
     * 判断是否支持解析器解析
     *
     * @return
     */
    @Override
    public Boolean isSupport(String classType) {
        return DataTypeUtil.isCollectionData(classType);
    }

    /**
     * 参数解析
     * @param objectStructure
     * @return
     */
    @Override
    public ParamData doResolve(ObjectStructure objectStructure) {
        if(ENABLE_DEBUG) {
            LOGGER.info(String.format("ListTypeStructureResolver begin.  type:%s, name:%s", objectStructure.getType(), objectStructure.getSimpleName()));
        }
        String paramName = StructureUtil.getTypeAlias(objectStructure);
        String commentText = StructureUtil.getCommentText(objectStructure);
        //提取泛型
        String genericTypeName = objectStructure.getGenericTypeName();
        ObjectStructure paramGenericType = objectStructure.getGenericType();
        if(Objects.isNull(paramGenericType)){
            //不存在泛型
            return super.createParamData(paramName, objectStructure.getType(), null, null, commentText, false);
        }
        if(DataTypeUtil.isCollectionData(genericTypeName)){
            throw new RuntimeException("List genericType can not be List ...");
        }

        List<ParamData> paramDataChildren = new ArrayList<>();
        ParamData paramData = super.doResolverChain(paramGenericType);

        Boolean isBaseData = DataTypeUtil.isBaseData(genericTypeName);
        paramDataChildren.add(paramData);

        return super.createParamData(paramName, objectStructure.getType(), genericTypeName, paramData.getParamValue(), commentText, !isBaseData);
    }

}
